//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.github.dongglin.smart.spring.boot.plus.core.framework.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.github.dongglin.smart.spring.boot.plus.common.enums.StatusCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.Serializable;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@Schema(description = "统一返回")
public class ApiResponse<T> implements Serializable {

    private static final long serialVersionUID = -62376414650226303L;
    @Schema(description = "响应编码")
    private String stateCode = "";
    @Schema(description = "响应信息")
    private String stateInfo = "";
    @Schema(description = "log")
    private String log = "";
    @Schema(description = "是否成功")
    private boolean success = false;
    @Schema(description = "响应时间戳")
    private long timestamp = System.currentTimeMillis();
    @Schema(description = "返回数据")
    private T data;

    /**
     * @param <T> s
     * @return s
     */
    public static <T> ApiResponse<T> success() {
        return success(null);
    }

    /**
     * @param data data
     * @param <T>  T
     * @return T
     */
    public static <T> ApiResponse<T> success(T data) {
        return success("", data);
    }

    /**
     * @param msg  msg
     * @param data d
     * @param <T>  t
     * @return t
     */
    public static <T> ApiResponse<T> success(String msg, T data) {
        return build(StatusCode.S0000000.getCode(), msg, "", data, true);
    }

    /**
     * @param msg msg
     * @param <T> t
     * @return t
     */
    public static <T> ApiResponse<T> success(String msg) {
        return build(StatusCode.S0000000.getCode(), msg == null ? StatusCode.S0000000.getMsg() : msg, "", null, true);
    }

    /**
     * @param <T> t
     * @return t
     */
    public static <T> ApiResponse<T> failure() {
        return failure(StatusCode.E0000000, "");
    }

    /**
     * @param statusCode s
     * @param <T>        t
     * @return t
     */
    public static <T> ApiResponse<T> failure(StatusCode statusCode) {
        return failure(statusCode, "");
    }

    /**
     * @param statusCode s
     * @param detailMsg  d
     * @param <T>        t
     * @return t
     */
    public static <T> ApiResponse<T> failure(StatusCode statusCode, String detailMsg) {
        return failure(statusCode.getCode(), statusCode.getMsg(), detailMsg);
    }

    /**
     * @param statusCode s
     * @param data       d
     * @param <T>        t
     * @return t
     */
    public static <T> ApiResponse<T> failure(StatusCode statusCode, T data) {
        return build(statusCode.getCode(), statusCode.getMsg(), "", data, false);
    }

    /**
     * @param msg msg
     * @param <T> t
     * @return t
     */
    public static <T> ApiResponse<T> failure(String msg) {
        return failure(StatusCode.BE0000000.getCode(), msg, "");
    }

    /**
     * @param stateInfo s
     * @param detailMsg m
     * @param <T>       t
     * @return t
     */
    public static <T> ApiResponse<T> failure(String stateInfo, String detailMsg) {
        return failure(StatusCode.BE0000000.getCode(), stateInfo, detailMsg);
    }

    /**
     * @param stateCode s
     * @param stateInfo s
     * @param detailMsg d
     * @param <T>       t
     * @return t
     */
    public static <T> ApiResponse<T> failure(String stateCode, String stateInfo, String detailMsg) {
        return build(stateCode, stateInfo, detailMsg, null, false);
    }

    /**
     * @param stateCode s
     * @param stateInfo s
     * @param ex        ex
     * @param <T>       t
     * @return t
     */
    public static <T> ApiResponse<T> failure(String stateCode, String stateInfo, Throwable ex) {
        return build(stateCode, stateInfo, ExceptionUtils.getStackTrace(ex), null, false);
    }

    /**
     * @param stateCode s
     * @param stateInfo s
     * @param ex        ex
     * @param data      da
     * @param <T>       t
     * @return t
     */
    public static <T> ApiResponse<T> failure(String stateCode, String stateInfo, Throwable ex, T data) {
        return build(stateCode, stateInfo, ExceptionUtils.getStackTrace(ex), data, false);
    }

    /**
     * @param stateCode s
     * @param stateInfo s
     * @param detailMsg d
     * @param data      d
     * @param success   s
     * @param <T>       t
     * @return t
     */
    public static <T> ApiResponse<T> build(String stateCode, String stateInfo, String detailMsg, T data, boolean success) {
        return new ApiResponse(stateCode, stateInfo, detailMsg, data, success);
    }

    /**
     * @param flag f
     * @return t
     */
    public static ApiResponse<Boolean> result(boolean flag) {
        return flag ? success() : failure();
    }

    /**
     * @return boolean
     */
    public Boolean isSuccess() {
        return StatusCode.S0000000.getCode().equals(this.stateCode);
    }

    /**
     * 构造
     */
    public ApiResponse() {
    }

    /**
     * @param stateCode s
     * @param stateInfo s
     * @param detailMsg d
     * @param data      d
     * @param success   s
     */
    private ApiResponse(String stateCode, String stateInfo, String detailMsg, T data, boolean success) {
        this.stateCode = stateCode;
        this.stateInfo = stateInfo;
        this.log = detailMsg;
        this.data = data;
        this.success = success;
        this.timestamp = System.currentTimeMillis();
    }
}
